package com.songcha.library_business.ui.dialog

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_picker.pickerview.adapter.ArrayWheelAdapter
import com.songcha.library_picker.pickerview.utils.LunarDateUtil
import com.songcha.library_picker.wheelview.view.WheelView
import com.songcha.library_business.R
import com.songcha.library_common.helper.ViewHelper

class ArrayPickerDialog(context: Context,val mTitle:String,val mList:List<String>, val mCurIndex:Int = 0,
                        val mIsBgTitle:Boolean = false,val mColor:Int? = null,val mCancelColor:Int? = null,
                        val mBlock:(Int)->Unit): CustomDialog(context) {

    init {
        setDialogTitle("")
        setDialogMsg("")
        setShowBtn(false)
        setWidth(312.dp2px())
        //setHeight(200.dp2px())
    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.business_dialog_array_picker,root,false)
        val tvTitle = view.findViewById<TextView>(R.id.business_tv_title)
        val wheel = view.findViewById<WheelView>(R.id.business_wheel)
        val btnCancel = view.findViewById<CustomButton>(R.id.business_btn_cancel)
        val btnConfirm = view.findViewById<CustomButton>(R.id.business_btn_confirm)
        val indicator = view.findViewById<View>(R.id.business_indicator)
        val llTitle = view.findViewById<LinearLayout>(R.id.business_ll_title)

        val flBgTitle = view.findViewById<FrameLayout>(R.id.business_fl_bg_title)
        val tvBgTitle = view.findViewById<TextView>(R.id.business_tv_bg_title)

        if(mIsBgTitle && mColor != null){
            ViewHelper.setVisibility(true,flBgTitle)
            ViewHelper.setVisibility(false,llTitle)
            tvBgTitle.text = mTitle
            flBgTitle.setBackgroundColor(mColor)
        }else{
            tvTitle.text = mTitle
        }


        if(mColor != null){
            indicator.setBackgroundColor(mColor)
            btnConfirm.setBtnBackgroundColor(mColor)
        }

        if(mCancelColor != null){
            btnCancel.setBtnBackgroundColor(mCancelColor)
        }

        btnCancel.setOnClickListener {
            dismiss()
        }
        btnConfirm.setOnClickListener{
            //mBlock(shichens[wheel.currentItem])
            //LunarDateUtil.getHourByLunarGanZhiIdx(wheel.currentItem)
            mBlock(wheel.currentItem)
            dismiss()
        }

        if(mList.isEmpty()) return view

        wheel.adapter = ArrayWheelAdapter(mList)

        wheel.apply {
            setCyclic(false)
            currentItem = mCurIndex
            setItemsVisibleCount(if(mList.size >= 5) 5 else mList.size)
            setDividerType(WheelView.DividerType.BACKGROUND)
            setTextColorCenter(Color.WHITE)
            setTextColorOut(Color.parseColor("#A0A0A0"))
            setTextSize(14f)
            setLineSpacingMultiplier(2.5f)
            if(mColor != null){
                setDividerColor(mColor)
            }else{
                setDividerColor(ColorUtil.getResourceColor(context,com.songcha.library_common.R.color.primary))
            }
        }

        return view
    }

    fun setCurrentIndex(index:Int){

    }

}